use std::{fmt::{Display, Formatter, Result}};

use crate::ast::ASTNode;

// token使用枚举类型，既可以表示单个类型，也可以表示有值的整数
#[derive(Clone, Copy)]
pub enum Token {
    Add,
    Substract,
    Multiply,
    Divide,
    Integer(i32),
    Eof,
}

impl Display for Token {
    fn fmt(&self, f: &mut Formatter) -> Result {
        match self {
            Token::Add => write!(f, "token: Add"),
            Token::Substract => write!(f, "token: Substract"),
            Token::Multiply => write!(f, "token: Multiply"),
            Token::Divide => write!(f, "token: Divide"),
            Token::Integer(number) => write!(f, "token: Integer, value: {}", number),
            Token::Eof => write!(f, "token: EOF"),
        }
    }
}

impl Token {
    fn token_2_astnode(self) -> Option<ASTNode> {
        match self {
            Token::Add => Some(ASTNode::Add),
            Token::Substract => Some(ASTNode::Substract),
            Token::Multiply => Some(ASTNode::Multiply),
            Token::Divide => Some(ASTNode::Divide),
            Token::Integer(number) => Some(ASTNode::Integer(number)),
            Token::Eof => None,
        }
    }
}